<?php


namespace App\Http\Controllers\Forwarder;

use App\Models\BadgeUser;
use App\Models\Forwarder;
use App\Models\ForwarderComplain;
use App\Models\ForwarderFollow;
use App\Models\ForwarderLine;
use App\Models\ForwarderStat;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ForwarderController extends Controller
{
    public function details(Request $request)
    {
        $forwarder = Forwarder::with(['forwarderStat'])->where('id', $request->input('id'))->first();
        $badge = BadgeUser::with(['badge'])->where('forwarder_id', $request->input('id'))->limit(4)->orderBy('sort', 'desc')->get();
        $complain = ForwarderComplain::where('forwarder_id', $request->input('id'))->count();
        $followObj = ForwarderFollow::where('forwarder_id', $request->input('id'))->where('user_id', $request->user()->id)->first();
        $follow = is_object($followObj) ? 1 : 0;
        $line = ForwarderLine::where('forwarder_id', $request->input('id'))->where('status', 1)->count();
        return $this->response(compact('forwarder','badge','complain','follow','line'));
    }

    public function index(Request $request)
    {
        $query = Forwarder::where('status', 1);
        if ($request->input('extends')){
            $query->with(explode(',', $request->input('extends')));
        }
        $obj = $query->paginate();
        return $this->response($obj);
    }

    public function view($id, Request $request){
        if ($id < 1){
            $id = $request->user()->forwarder_id;
        }
        $query = Forwarder::where('id', $id);
        if ($request->input('extends')){
            $query->with(explode(',', $request->input('extends')));
        }
        $obj = $query->first();
        return $this->response($obj);
    }

    public function checkApply(Request $request){
        $obj = Forwarder::where('user_id', $request->user()->id)->where('status', 0)->first();
        return $this->response(is_object($obj) ? 1 : 0);
    }

    public function store(Request $request)
    {
        $user_id = $request->user()->id;
        $obj = Forwarder::where('user_id', $request->user()->id)->first();
        if (!is_object($obj)){
            $obj = new Forwarder();
        }
        $all = $request->all();
        $obj->fill($all);
        $obj->user_id =$user_id;
        $obj->status = 0;
        $obj->qrcode = $this->getQrcode($obj->id);
        $forwarderStat = new ForwarderStat();
        $obj->save();
        $obj->forwarderStat()->save($forwarderStat);
        return $this->response();
    }

    public function getQrcode($id){
        $app = Factory::miniProgram(config('wechat'));
        $response = $app->app_code->get('home/home?forwarder_follow=' . $id);
        $filename = $id . '.png';
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $response->save('../storage/app/public/qrcode', $filename);
        }
        return env('APP_URL') . '/storage/qrcode/' . $filename;
    }

    public function update(Request $request)
    {
        $obj = Forwarder::where('id', $request->user()->forwarder_id)->first();
        $obj->update($request->all());
        return $this->response();
    }

    public function destroy($id)
    {
        $obj = Forwarder::find($id);
        $obj->delete();
        return $this->response();
    }
}
